package com.xyht.sca_s.student_manage_system.modules.waterMark.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.waterMark.entity.req.SmsWaterMarkRecordReq;
import com.xyht.sca_s.student_manage_system.modules.waterMark.service.SmsWaterMarkService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 合同水印院校图标 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/waterMark")
@SecretBody
public class SmsWaterMarkController {
    @Resource
    private SmsWaterMarkService smsWaterMarkService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "pdf文件添加水印")
    @PostMapping("/add")
    public ResponseResult pdfWaterMarkAdd(@RequestParam(value = "name") String name,
                                          @RequestParam(value = "nameSpace") Integer nameSpace,
                                          @RequestParam(value = "opacity") Float opacity,
                                          @RequestParam(value = "imgSize") Integer imgSize,
                                          @RequestParam(value = "colorR") Integer colorR,
                                          @RequestParam(value = "colorG") Integer colorG,
                                          @RequestParam(value = "colorB") Integer colorB,
                                          @RequestParam(value = "fontSize", required = false) Float fontSize,
                                          @RequestParam("pdfFile") MultipartFile pdfFile,
                                          @RequestParam(value = "logoFile", required = false) MultipartFile logoFile,
                                          HttpServletRequest request,
                                          HttpServletResponse response) {
        ResponseResult responseResult;
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        try {
            Future<ResponseResult> future = smsWaterMarkService.pdfWaterMarkAdd(user_id, name, nameSpace, opacity,
                    imgSize, colorR, colorG, colorB, fontSize,
                    pdfFile, logoFile, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "合同水印记录列表")
    @GetMapping("/recordList")
    public ResponseResult getWaterMarkRecord(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWaterMarkService.getWaterMarkRecord(pageNum, pageSize, user_id);
    }

    @ApiModelProperty(value = "删除水印记录")
    @PostMapping("/recordDelete")
    public ResponseResult getWaterMarkRecord(@RequestBody SmsWaterMarkRecordReq recordReq,HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWaterMarkService.deleteWaterMartRecord(recordReq, user_id);
    }
}

